package com.pipayshop.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 订单超时未支付，延迟队列配置
 */
@Configuration
public class OrderQueueConfig {

    // 普通交换机名称（shop模块）
    public static final String SHOP_ORDER_EXCHANGE = "shop_order_exchange";
    // 死信交换机名称（shop模块）
    public static final String SHOP_DEAD_EXCHANGE = "shop_dead_exchange";
    // 普通队列名称（shop模块）
    public static final String SHOP_ORDER_QUEUE = "shop_order_queue";
    // 延迟队列名称（shop模块）
    public static final String SHOP_DELAY_QUEUE = "shop_delay_queue";
    public static final String SHOP_DEAD_LETTER_QUEUE = "shop_dead_letter_queue";

    // 普通交换机名称（store模块）
    public static final String STORE_ORDER_EXCHANGE = "store_order_exchange";
    // 死信交换机名称（store模块）
    public static final String STORE_DEAD_EXCHANGE = "store_dead_exchange";
    // 普通队列名称（store模块）
    public static final String STORE_ORDER_QUEUE = "store_order_queue";
    // 延迟队列名称（store模块）
    public static final String STORE_DELAY_QUEUE = "store_delay_queue";
    public static final String STORE_DEAD_LETTER_QUEUE = "store_dead_letter_queue";

    // 普通交换机名称（hotel模块）
    public static final String HOTEL_ORDER_EXCHANGE = "hotel_order_exchange";
    // 死信交换机名称（hotel模块）
    public static final String HOTEL_DEAD_EXCHANGE = "hotel_dead_exchange";
    // 普通队列名称（hotel模块）
    public static final String HOTEL_ORDER_QUEUE = "hotel_order_queue";
    // 延迟队列名称（hotel模块）
    public static final String HOTEL_DELAY_QUEUE = "hotel_delay_queue";
    public static final String HOTEL_DEAD_LETTER_QUEUE = "hotel_dead_letter_queue";


    // 1. 声明普通交换机
    @Bean("shopOrderExchange")
    public DirectExchange shopOrderExchange() {
        return new DirectExchange(SHOP_ORDER_EXCHANGE);
    }

    @Bean("storeOrderExchange")
    public DirectExchange storeOrderExchange() {
        return new DirectExchange(STORE_ORDER_EXCHANGE);
    }

    @Bean("hotelOrderExchange")
    public DirectExchange hotelOrderExchange() {
        return new DirectExchange(HOTEL_ORDER_EXCHANGE);
    }

    // 2. 声明死信交换机
    @Bean("shopDeadExchange")
    public DirectExchange shopDeadExchange() {
        return new DirectExchange(SHOP_DEAD_EXCHANGE);
    }

    @Bean("storeDeadExchange")
    public DirectExchange storeDeadExchange() {
        return new DirectExchange(STORE_DEAD_EXCHANGE);
    }

    @Bean("hotelDeadExchange")
    public DirectExchange hotelDeadExchange() {
        return new DirectExchange(HOTEL_DEAD_EXCHANGE);
    }

    // 3. 声明延迟队列
    @Bean("shopDelayQueue")
    public Queue shopDelayQueue() {
        return QueueBuilder.durable(SHOP_DELAY_QUEUE).build();
    }

    @Bean("storeDelayQueue")
    public Queue storeDelayQueue() {
        return QueueBuilder.durable(STORE_DELAY_QUEUE).build();
    }

    @Bean("hotelDelayQueue")
    public Queue hotelDelayQueue() {
        return QueueBuilder.durable(HOTEL_DELAY_QUEUE).build();
    }

    // 4. 声明普通队列（并与死信队列绑定）
    @Bean("shopOrderQueue")
    public Queue shopOrderQueue() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("x-dead-letter-exchange", SHOP_DEAD_EXCHANGE);
        param.put("x-dead-letter-routing-key", SHOP_DEAD_LETTER_QUEUE);
        return QueueBuilder.durable(SHOP_ORDER_QUEUE).withArguments(param).build();
    }

    @Bean("storeOrderQueue")
    public Queue storeOrderQueue() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("x-dead-letter-exchange", STORE_DEAD_EXCHANGE);
        param.put("x-dead-letter-routing-key", STORE_DEAD_LETTER_QUEUE);
        return QueueBuilder.durable(STORE_ORDER_QUEUE).withArguments(param).build();
    }

    @Bean("hotelOrderQueue")
    public Queue hotelOrderQueue() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("x-dead-letter-exchange", HOTEL_DEAD_EXCHANGE);
        param.put("x-dead-letter-routing-key", HOTEL_DEAD_LETTER_QUEUE);
        return QueueBuilder.durable(HOTEL_ORDER_QUEUE).withArguments(param).build();
    }

    // 5. 普通队列与交换机绑定
    @Bean
    public Binding shopOrderQueueBinding(@Qualifier("shopOrderQueue") Queue queue,
                                         @Qualifier("shopOrderExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("shop");
    }

    @Bean
    public Binding storeOrderQueueBinding(@Qualifier("storeOrderQueue") Queue queue,
                                          @Qualifier("storeOrderExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("store");
    }

    @Bean
    public Binding hotelOrderQueueBinding(@Qualifier("hotelOrderQueue") Queue queue,
                                          @Qualifier("hotelOrderExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("hotel");
    }

    // 6. 死信队列与死信交换机绑定
    @Bean
    public Binding shopDelayQueueBinding(@Qualifier("shopDelayQueue") Queue queue,
                                         @Qualifier("shopDeadExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(SHOP_DEAD_LETTER_QUEUE);
    }

    @Bean
    public Binding storeDelayQueueBinding(@Qualifier("storeDelayQueue") Queue queue,
                                          @Qualifier("storeDeadExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(STORE_DEAD_LETTER_QUEUE);
    }

    @Bean
    public Binding hotelDelayQueueBinding(@Qualifier("hotelDelayQueue") Queue queue,
                                          @Qualifier("hotelDeadExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(HOTEL_DEAD_LETTER_QUEUE);
    }
}
